package com.xmh.ssm.Interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class LoginInterceptor extends HandlerInterceptorAdapter {
	
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		super.afterCompletion(request, response, handler, ex);
	}
 
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		super.postHandle(request, response, handler, modelAndView);
	}
 
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		request.setCharacterEncoding("UTF-8");
        String url = request.getServletPath();    
        System.out.println("post URL锛�"+url);
        if(!url.equals("")){
            //鍒ゆ柗鏄惁宸茬櫥褰�
           String username = (String) request.getSession().getAttribute("username");
            if(username == null){
                //鐒ession鍓囨槸鏈櫥褰曠媭鎱�
                System.out.println(">>>鏈櫥褰曪紝璜嬮噸鏂扮櫥褰�<<<");
                response.sendRedirect("http://localhost:8082/SSM/loginTest/loginPage.do?actionUrl=" + url);
                return false;
            }
        }
        return true;
	}
	
	private boolean isPassUrl(String url){
        if(!url.endsWith("/login/login") 
                && !url.endsWith("/login/chnagePassEntry")
                && !url.endsWith("/login/change_Login")
                && !url.endsWith("/API/Service")
                && !url.endsWith("/API/Service2")
                && !url.endsWith("/province.txt")
                && !url.endsWith("/city.txt")
                && !url.endsWith("/area.txt")
                && !url.endsWith(".xml") 
                && !url.endsWith(".js") 
                && !url.endsWith(".css") && !url.endsWith(".png")
                && !url.endsWith(".CSS") 
                && !url.endsWith(".jpg") && !url.endsWith(".gif")
                && !url.endsWith(".JPG") && !url.endsWith(".GIF")){
            return true;
        }
        return false;
    }
	

}
